// 导航栏登录注册按钮
login_blank();
function login_blank() {
  $('.login_registerer').click(function (e) {
    e.preventDefault();
    $('.login-blank').css('display', 'block');
    $('.blank').css('display', 'block');
  });

  $('[name="register"]').click(function () {
    $('.login-1').css('display', 'none');
    $('.register-1').css('display', 'block');
  });
  $('.cancel-register').click(function () {
    $('.login-blank').css('display', 'none');
    $('.login-1').css('display', 'block');
    $('.register-1').css('display', 'none');
    $('.blank').css('display', 'none');
  });
}
/*
购物车
*/
$('[came="cart"]').click(function (e) {
  if (window.localStorage.login === undefined) {
    $('.login-blank').css('display', 'block');
    $('.blank').css('display', 'block');
    login_blank();
    login()
    e.preventDefault();
    return
    
  }else if(window.localStorage.login === 1){
  window.location.href='../pages/cart.html'

  }
  
});

/*

导航

*/
const arr = ['居家生活', '厨房电器', '食品酒水', '电脑办公'];

let str = `
<li class="city">
<a href="#">
  <img src="https://mall.mcake.com/static/img/icon05.png" alt="" />
  <span>上海</span>
</a>
</li>
<li class="product">
<a href="#">
  <img src="https://mall.mcake.com/static/img/icon01.png" alt="" />
  <span>所有产品</span>
</a>
</li>`;
arr.forEach(function (item) {
  str += `
    <li class="product">
      <a href="../pages/list.html?cat_one_id=${item}"">
        <img src="https://mall.mcake.com/static/img/icon01.png" alt="" />
        <span>${item}</span>
      </a>
    </li>`;
});

$('[ name="nav_Ul"]').html(str);

/*
注册界面

*/
$('#sc')
  .html(setVC())
  .click(function (e) {
    $('#sc').html(setVC());
    e.preventDefault();
  });
// 账号input添加change事件
$('[name="register-name"]').change(function () {
  nameSelect();
});
async function nameSelect() {
  let name = $('[name="register-name"]').val();

  let res = await myPromiseJQueryAjax(
    '../server/goods_select.php',
    'post',
    { userName: name },
    'json'
  );
  if (res.code === 1) {
    $('[name="reg_Name_Span"]').html('您的账号可以使用').css('color', 'blue');
  } else if (res.code === 0) {
    $('[name="reg_Name_Span"]').html('账号名重复').css('color', 'red');
  }
}

//注册账号
$('.confirm-register').click(function () {
  register();
});

async function register() {
  let name = $('[name="register-name"]').val();
  let pwd1 = $('[name="register-psw1"]').val();
  let pwd2 = $('[name="register-psw2"]').val();
  let vc = $('[name="vc"]').val();
  console.log(pwd1.length);

  if (pwd1 === pwd2) {
    $('[name="reg_Psw_Span"]').html('');
  } else if (!(pwd1 === pwd2)) {
    $('[name="reg_Psw_Span"]').html('两次输入密码不一样').css('color', 'red');
    return;
  } else if (name.length === 0 || pwd1.length === 0 || pwd2.length === 0 || vc.length === 0) {
    window.alert('输入框不能为空');
    return;
  }

  if (vc.toLowerCase() === $('#sc').html().toLowerCase()) {
    $('[name="vc-text"]').html('');
  } else {
    $('[name="vc-text"]').html('您输入的验证码错误').css('color', 'red');
    $('#sc').html(setVC());
    $('[name="vc"]').val('');
    let time = setTimeout(() => {
      $('[name="vc-text"]').html('');
    }, 3000);

    clearInterval(time);
    return;
  }

  const res = await myPromiseJQueryAjax(
    '../server/goods_register.php',
    'post',
    { userName: name, userPwd: pwd1 },
    'json'
  );
  console.log(res);

  if (res.code === 1) {
    // localStorage.id = res.id;
    $('.login-1').css('display', 'block');
    $('.register-1').css('display', 'none');
    $('[name="name"]').val(name);
    $('[name="psw"]').val(pwd1);
  } else if (res.code === 0) {
    window.alert('您注册失败，请重新注册');
  }
}
/*

登录界面

*/
//验证码
$('[name="login-vc"]')
  .html(setVC())
  .click(function (e) {
    e.preventDefault();
    $('[name="login-vc"]').html(setVC());
  });
//登录按钮添加事件
$('[name="login"]').click(function () {
  login();
});

async function login() {
  let name = $('[name="name"]').val();
  let pwd = $('[name="psw"]').val();
  let vc = $('.login-vc').val();

  if (vc.toLowerCase() === $('[name="login-vc"]').html().toLowerCase()) {
    $('.login-vc-span').html('');
  } else {
    $('.login-vc-span').html('您输入的验证码错误').css('color', 'red');
    $('[name="login-vc"]').html(setVC());
    $('.login-vc').val('');
    let time = setTimeout(() => {
      $('.login-vc-span').html('');
    }, 3000);
    return;
  }

  const res = await myPromiseJQueryAjax(
    '../server/goods_login.php',
    'post',
    { userName: name, userPwd: pwd },
    'json'
  );
  console.log(res);
  if (res.code === 1) {
    window.localStorage.login = 1;

    window.location.reload();
    $('[ name="login_Register"]').html('');
  } else if (res.code === 0) {
    window.alert('登录失败');
  }
}
